package com.lineage.server.serverpackets;

import com.lineage.server.datatables.lock.ClanReading;
import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class S_Pledge extends ServerBasePacket {
  private byte[] _byte;
  
  public S_Pledge(int ClanId) {
    this._byte = null;
    L1Clan clan = ClanReading.get().getTemplate(ClanId);
    writeC(250);
    writeC(167);
    writeS(clan.getClanName());
    writeS(clan.getLeaderName());
    writeD(clan.getEmblemId());
    writeD((int)(clan.getFoundDate().getTime() / 1000L));
    try {
      byte[] text = new byte[478];
      Arrays.fill(text, (byte)0);
      int i = 0;
      byte[] bytes;
      int length = (bytes = clan.getAnnouncement().getBytes("Big5")).length;
      int j = 0;
      while (j < length) {
        byte b = bytes[j];
        text[i++] = b;
        j++;
      } 
      writeByte(text);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } 
  }
  
  public S_Pledge(int page, int current_page, ArrayList<String> list) {
    this._byte = null;
    writeC(250);
    writeC(170);
    writeC(page);
    writeC(current_page);
    writeC(list.size());
    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {
      String name = iterator.next();
      if (name == null)
        continue; 
      try {
        L1PcInstance clanMember = CharacterTable.get().restoreCharacter(name);
        writeS(clanMember.getName());
        writeC(clanMember.getClanRank());
        byte[] text = new byte[62];
        Arrays.fill(text, (byte)0);
        if (clanMember.getClanMemberNotes() != null) {
          int i = 0;
          byte[] bytes;
          int length = (bytes = clanMember.getClanMemberNotes().getBytes("Big5")).length;
          int j = 0;
          while (j < length) {
            byte b = bytes[j];
            text[i++] = b;
            j++;
          } 
        } 
        writeByte(text);
        writeD(clanMember.getClanMemberId());
        writeC(clanMember.getType());
      } catch (Exception exception) {}
    } 
  }
  
  public S_Pledge(String name, String notes) {
    this._byte = null;
    writeC(250);
    writeC(169);
    writeS(name);
    byte[] text = new byte[62];
    Arrays.fill(text, (byte)0);
    if (notes.length() != 0) {
      int i = 0;
      try {
        byte[] bytes;
        int length = (bytes = notes.getBytes("Big5")).length;
        int j = 0;
        while (j < length) {
          byte b = bytes[j];
          text[i++] = b;
          j++;
        } 
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      } 
    } 
    writeByte(text);
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
